﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Input;
using System.Windows.Forms;
using System.Drawing;
using System.CodeDom.Compiler;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UITesting;
using Microsoft.VisualStudio.TestTools.UITest.Extension;
using Keyboard = Microsoft.VisualStudio.TestTools.UITesting.Keyboard;
using Mouse = Microsoft.VisualStudio.TestTools.UITesting.Mouse;
using MouseButtons = System.Windows.Forms.MouseButtons;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CodedUITestProject1
{
    public class globalDOTNETFunctions
    {
        public static bool Button_Click(string oWinName,string oWinClassName,string oBtnName,string oBtnType)   // generic function for clicking a button
        {

            Boolean bolBtnExists;

            WinWindow uiwin = new WinWindow();
            WinButton uibtn = new WinButton(uiwin);

            uiwin.SearchProperties[WinWindow.PropertyNames.Name] = oWinName;
            uiwin.SearchProperties[WinWindow.PropertyNames.ClassName] = oWinClassName;

            if (uiwin.Exists)
            {

                uibtn.SearchProperties[WinButton.PropertyNames.Name] = oBtnName;
                uibtn.SearchProperties[WinButton.PropertyNames.ControlType] = oBtnType;


                bolBtnExists = uibtn.Exists;

                if (bolBtnExists)
                {
                    Mouse.Click(uibtn);
                    return true;
                }
                else
                {
                    Assert.IsTrue(bolBtnExists, "Button - " + oBtnName + " - not found on the " + oWinName + " window. Please verify object repository or the properties of the control in the application");
                    return false;

                }
            }

            else
            {
                Assert.IsTrue(uiwin.Exists, "Window - " + oWinName + " - not found on the application. Please verify object repository or the properties of the control in the application");
                return false;
            }

            
        }

        public static bool Textbox_EnterText(string oWinName, string oWinControlType, string oTxtboxName, string oTxtboxControlType, string strValue)   // generic function for entering value in a textbox
        {
            
            Boolean bolTxtboxExists;

            WinWindow uiwin = new WinWindow();
            WinEdit uiTxtbox = new WinEdit(uiwin);

            uiwin.SearchProperties[WinWindow.PropertyNames.Name] = oWinName;
            uiwin.SearchProperties[WinWindow.PropertyNames.ControlType] = oWinControlType;

            if (uiwin.Exists)
            {
                uiTxtbox.SearchProperties[WinButton.PropertyNames.Name] = oTxtboxName;
                uiTxtbox.SearchProperties[WinButton.PropertyNames.ControlType] = oTxtboxControlType;

                bolTxtboxExists = uiTxtbox.Exists;

                if (bolTxtboxExists)
                {
                    uiTxtbox.Text = strValue;
                    return true;
                }
                else
                {
                    Assert.IsTrue(bolTxtboxExists, "Textbox - " + oTxtboxName + " not found on the " + oWinName + " - Window. Please verify object repository or the properties of the control in the application");
                    return false;

                }
            }
            else
            {
                Assert.IsTrue(uiwin.Exists, "Window - " + oWinName + " - not found on the application. Please verify object repository or the properties of the control in the application");
                return false;
            }

        }

        public static bool Combo_SelectValue(string oWinName, string oWinControlType, string oComboName, string oComboControlType, string strValue) // generic function for selecting a value from a combobox
        {
      
            Boolean bolComboboxExists;

            WinWindow uiwin = new WinWindow();
            WinComboBox uiCombo = new WinComboBox(uiwin);

            uiwin.SearchProperties[WinWindow.PropertyNames.Name] = oWinName;
            uiwin.SearchProperties[WinWindow.PropertyNames.ControlType] = oWinControlType;

            if (uiwin.Exists)
            {

                uiCombo.SearchProperties[WinButton.PropertyNames.Name] = oComboName;
                uiCombo.SearchProperties[WinButton.PropertyNames.ControlType] = oComboControlType;


                bolComboboxExists = uiCombo.Exists;

                if (bolComboboxExists)
                {
                    uiCombo.SelectedItem = strValue;
                    return true;
                }
                else
                {
                    Assert.IsTrue(bolComboboxExists, "ComboBox - " + oComboName + " not found on the " + oWinName + " - Window. Please verify object repository or the properties of the control in the application");
                    return false;

                }
            }
            else
            {
                Assert.IsTrue(uiwin.Exists, "Window - " + oWinName + " - not found on the application. Please verify object repository or the properties of the control in the application");
                return false;
            }

        }

        public static bool CheckBox_Select(string oWinName, string oWinControlType, string oCheckboxName, string oCheckboxControlType)  // generic function for selecting a checkbox
        {
          
            Boolean bolCheckboxExists;

            WinWindow uiwin = new WinWindow();
            WinCheckBox uiCheckbox = new WinCheckBox(uiwin);

            uiwin.SearchProperties[WinWindow.PropertyNames.Name] = oWinName;
            uiwin.SearchProperties[WinWindow.PropertyNames.ControlType] = oWinControlType;

            if (uiwin.Exists)
            {
                uiCheckbox.SearchProperties[WinButton.PropertyNames.Name] = oCheckboxName;
                uiCheckbox.SearchProperties[WinButton.PropertyNames.ControlType] = oCheckboxControlType;


                bolCheckboxExists = uiCheckbox.Exists;

                if (bolCheckboxExists)
                {
                    uiCheckbox.Checked = true;
                    return true;
                }
                else
                {
                    Assert.IsTrue(bolCheckboxExists, "CheckBox - " + oCheckboxName + " not found on the " + oWinName + " - Window. Please verify object repository or the properties of the control in the application");
                    return false;

                }
            }
            else
            {
                Assert.IsTrue(uiwin.Exists, "Window - " + oWinName + " - not found on the application. Please verify object repository or the properties of the control in the application");
                return false;
            }
        }

        public static bool Table_SelectRow(string oWinName, string oWinControlType, string oTableName, string oTableControlType,string iRow)    // generic function for selecting a row in a table/grid
        {

            Boolean bolTableExists;

            WinWindow uiwin = new WinWindow();
            WinTable uiTable = new WinTable(uiwin);
            WinRowHeader uiRow = new WinRowHeader (uiTable);

            uiwin.SearchProperties[WinWindow.PropertyNames.Name] = oWinName;
            uiwin.SearchProperties[WinWindow.PropertyNames.ControlType] = oWinControlType;

            if (uiwin.Exists)
            {
                uiTable.SearchProperties[WinTable.PropertyNames.Name] = oTableName;
                uiTable.SearchProperties[WinTable.PropertyNames.ControlType] = oTableControlType;


                bolTableExists = uiTable.Exists;

                if (bolTableExists)
                {               
                    uiRow.SearchProperties[WinRowHeader.PropertyNames.Name] = "Row "+iRow;
                    Mouse.Click(uiRow, new Point(8, 8));
                    return true;
                }
                else
                {
                    Assert.IsTrue(bolTableExists, "Table - " + oTableName + " not found on the " + oWinName + " - Window. Please verify object repository or the properties of the control in the application");
                    return false;

                }
            }
            else
            {
                Assert.IsTrue(uiwin.Exists, "Window - " + oWinName + " - not found on the application. Please verify object repository or the properties of the control in the application");
                return false;
            }
        }
    }
}
